package com.ozo.easy.gen.controller;

import com.ozo.easy.common.log.annotation.LogAnno;
import com.ozo.easy.common.model.BaseBatchDTO;
import com.ozo.easy.common.model.BasePage;
import com.ozo.easy.common.model.PageResult;
import com.ozo.easy.common.model.ResultVO;
import com.ozo.easy.gen.pojo.dto.GenTableAddDTO;
import com.ozo.easy.gen.pojo.dto.GenTableDetailDTO;
import com.ozo.easy.gen.pojo.dto.GenTableImportDTO;
import com.ozo.easy.gen.pojo.dto.GenTableQueryDTO;
import com.ozo.easy.gen.pojo.vo.GenTableVO;
import com.ozo.easy.gen.service.IGenTableService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * <p>
 * 表配置 前端控制器
 * </p>
 *
 * @author ozo
 * @since 2025-01-07
 */
@Tag(name = "表配置管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/genTable")
public class GenTableController {

    private final IGenTableService genTableService;

    @Operation(summary = "表格页")
    @GetMapping("/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("gen/index");
        return modelAndView;
    }

    @Operation(summary = "新建页")
    @GetMapping("/addForm")
    public ModelAndView addForm() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("gen/addForm");
        return modelAndView;
    }

    @Operation(summary = "导入页")
    @GetMapping("/importForm")
    public ModelAndView importForm() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("gen/importForm");
        return modelAndView;
    }

    @Operation(summary = "配置页")
    @GetMapping("/editForm/{id}")
    public ModelAndView editForm(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("data", genTableService.detail(id));
        modelAndView.setViewName("gen/editForm");
        return modelAndView;
    }

    @LogAnno(value = "导入表", module = "代码生成")
    @Operation(summary = "导入表")
    @PostMapping("/import")
    public ResultVO<Void> importTable(@RequestBody @Valid GenTableImportDTO tableImportDTO) {
        genTableService.importTable(tableImportDTO);
        return ResultVO.success();
    }

    @LogAnno(value = "新建表", module = "代码生成")
    @Operation(summary = "新建表")
    @PostMapping("/add")
    public ResultVO<Void> add(@RequestBody @Valid GenTableAddDTO addDTO) {
        genTableService.add(addDTO);
        return ResultVO.success();
    }

    @LogAnno(value = "删除表", module = "代码生成")
    @Operation(summary = "删除表")
    @DeleteMapping("/remove")
    public ResultVO<Void> remove(@RequestBody @Valid BaseBatchDTO<Long> batchDTO) {
        genTableService.remove(batchDTO);
        return ResultVO.success();
    }

    @LogAnno(value = "修改配置", module = "代码生成")
    @Operation(summary = "修改配置")
    @PutMapping("/edit")
    public ResultVO<Void> edit(@RequestBody @Valid GenTableDetailDTO saveDTO) {
        genTableService.edit(saveDTO);
        return ResultVO.success();
    }

    @Operation(summary = "表分页")
    @GetMapping("/page")
    public ResultVO<PageResult<GenTableVO>> page(@Valid BasePage basePage, GenTableQueryDTO queryDTO) {
        return ResultVO.success(genTableService.page(basePage, queryDTO));
    }

    @Operation(summary = "表详情")
    @GetMapping("/{id}")
    public ResultVO<GenTableDetailDTO> detail(@PathVariable Long id) {
        return ResultVO.success(genTableService.detail(id));
    }

    @LogAnno(value = "代码生成", module = "代码生成")
    @Operation(summary = "代码生成")
    @PostMapping("/genLocal/{id}")
    public ResultVO<Void> genLocal(@PathVariable Long id) {
        genTableService.generateLocal(id);
        return ResultVO.success();
    }

    @LogAnno(value = "代码下载", module = "代码生成")
    @Operation(summary = "代码下载")
    @GetMapping("/genDownload/{id}")
    public void genDownload(@PathVariable Long id, HttpServletResponse response) {
        genTableService.generateDownload(id, response);
    }

    @Operation(summary = "数据库表分页")
    @GetMapping("/pageDbTable")
    public ResultVO<PageResult<GenTableVO>> pageDbTable(@Valid BasePage basePage, GenTableQueryDTO queryDTO) {
        return ResultVO.success(genTableService.pageDbTable(basePage, queryDTO));
    }

}
